package com.tuyan.aixiang.activity.home.control;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.api.HttpClient;
import com.tuyan.aixiang.api.helper.ResultTransformer;
import com.tuyan.aixiang.api.helper.RxSchedulers;
import com.tuyan.aixiang.common.BasePresenter;
import com.tuyan.aixiang.common.Constant;
import com.tuyan.aixiang.common.M;
import com.tuyan.aixiang.common.StorageKey;
import com.tuyan.aixiang.common.lang.ListHelper;
import com.tuyan.aixiang.model.DeviceNumberResult;
import com.tuyan.aixiang.model.Mode;
import com.tuyan.aixiang.model.ShortCut;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.StorageUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

import static com.tuyan.aixiang.common.Constant.SHORT_CUT_ALARM;
import static com.tuyan.aixiang.common.Constant.SHORT_CUT_MSG;
import static com.tuyan.aixiang.common.Constant.SHORT_CUT_NOTICE;
import static com.tuyan.aixiang.common.Constant.SHORT_CUT_SHARE;

public class ControlPresenter extends BasePresenter<Control.View> implements Control.Presenter {

    @Override
    public void register(Control.View view) {
        super.register(view);
        fetchWeatherInfo();
        refetch();
    }

    void fetchWeatherInfo() {
    }

    void fetchModeList() {
        List<Mode> modes = new ArrayList<>();
        iView.get().loadModeList(modes);
    }

    void fetchDeviceList() {
        iView.get().initNoDevice();
        noCamera = false;
        noAlarm = false;
        fetchCameraDevice();
        fetchSensorDevice();
        fetchDeviceNumber();
    }

    void fetchDeviceNumber() {
        Disposable disposable = HttpClient.getDevice().deviceNumber().compose(RxSchedulers.io_main())
                .compose(ResultTransformer.handleResult())
                .subscribe(data -> {
                    DeviceNumberResult result = data.get();
                    if (null != result) {
                        iView.get().showVidSize(result.getCamera());
                        iView.get().showSensorSize(result.getAlarm() + result.getSocket());
                    }
                }, throwable -> {

                });
        addDisposable(disposable);
    }

    boolean noCamera = false;
    boolean noAlarm = false;
//    boolean noSocket = false;

    void fetchCameraDevice() {
        Disposable disposable = HttpClient.getDevice().devicePage(M.m().put("dr_model", Constant.MODEL_CAMERA).put("page", 1))
                .compose(RxSchedulers.io_main())
                .compose(ResultTransformer.handleResult())
                .subscribe(data -> {
                    if (null == data.get()) {
                        noCamera = true;
                    } else {
                        List<YsDevice> vidDevices = data.get().getList();
                        if (vidDevices.size() == 0) {
//                            iView.get().initNoDevice();
                            // fall through
                            noAlarm = true;
                        } else {
                            List<YsDevice> limitVids = ListHelper.limit(vidDevices, 2);
                            iView.get().loadVidPanel(limitVids);

//                            List<YsDevice> limitSensor = ListHelper.limit(sensorDevices, 2);
//                            iView.get().loadSensorPanel(limitSensor);
                        }
                    }
                    checkNoDevice();

                }, throwable -> {
                    iView.get().initNoDevice();
                    noCamera = true;
                    checkNoDevice();
                });
        addDisposable(disposable);
    }

    void fetchSensorDevice() {


        Disposable disposable = HttpClient.getDevice().devicePage(M.m().put("dr_model", Constant.MODEL_ALARM).put("page", 1))
                .compose(RxSchedulers.io_main())
                .compose(ResultTransformer.handleResult())
                .subscribe(data -> {
                    if (null == data.get()) {
                        noAlarm = true;
                    } else {
                        List<YsDevice> vidDevices = data.get().getList();
                        if (vidDevices.size() == 0) {
//                            iView.get().initNoDevice();
                            // fall through
                            noAlarm = true;
                        } else  {
                            List<YsDevice> limitVids = ListHelper.limit(vidDevices, 2);
                            iView.get().loadSensorPanel(limitVids);

                        }
//                        Disposable disposable2 = HttpClient.getDevice().devicePage(M.m().put("dr_model", Constant.MODEL_SOCKET))
//                                .compose(RxSchedulers.io_main())
//                                .compose(ResultTransformer.handleResult())
//                                .subscribe(socketData -> {
//                                    List<YsDevice> socketDevices = socketData.get().getList();
//
//                                    if (socketDevices.size() == 0) {
//                                        noSocket = true;
//                                        iView.get().loadSensorPanel(vidDevices);
//                                    } else {
//                                        socketDevices.addAll(vidDevices);
//                                        socketDevices = ListHelper.limit(socketDevices, 2);
//                                        iView.get().loadSensorPanel(socketDevices);
//                                    }
//                                    checkNoDevice();
//                                }, throwable -> {
//                                    noSocket = true;
//                                    iView.get().loadSensorPanel(vidDevices);
//                                    checkNoDevice();
//                                });
//                        addDisposable(disposable2);

                    }
                    checkNoDevice();

                }, throwable -> {
                    iView.get().initNoDevice();
                    noAlarm = true;
                    checkNoDevice();
                });
        addDisposable(disposable);
    }

    void checkNoDevice() {
        if (noAlarm && noCamera) {
            iView.get().initNoDevice();
        }

    }


    private ShortCut getShortCut(JsonElement ele) {
//        if(ele.getAsInt())
        Integer type = ele.getAsInt();
        ShortCut shortCut = new ShortCut();
        switch (type) {
            case SHORT_CUT_ALARM:
                shortCut.setIconRes(R.drawable.icon_alarm);
                shortCut.setTitleRes(R.string.shortcut_alarm);
                shortCut.setBgColor("#FFF7F2");
                shortCut.setUrl(Constant.NOTICE_LIST);
                shortCut.setShowMsgCount(true);
                break;
            case SHORT_CUT_MSG:
                shortCut.setIconRes(R.drawable.icon_msg);
                shortCut.setTitleRes(R.string.shortcut_msg);
                shortCut.setBgColor("#FFFCF2");
                break;
            case SHORT_CUT_NOTICE:
                shortCut.setIconRes(R.drawable.icon_notice);
                shortCut.setTitleRes(R.string.shortcut_notice);
                shortCut.setBgColor("#EEF5E9");
                break;
            case SHORT_CUT_SHARE:
                shortCut.setIconRes(R.drawable.icon_share);
                shortCut.setTitleRes(R.string.shortcut_share_device);
                shortCut.setBgColor("#F2FCFF");
                shortCut.setUrl(Constant.ME_SHARE_DEVICE);
                break;
        }
        return shortCut;
    }


    void getControlList() {
        List<ShortCut> shortCuts = new ArrayList<>();
        JsonArray jsonArray = StorageUtil.getObject(StorageKey.SHORT_CUT, JsonArray.class);
        if (null != jsonArray) {
            for (int i = 0; i < jsonArray.size(); i++) {
                shortCuts.add(getShortCut(jsonArray.get(i)));
            }
        }
        iView.get().loadControlPanel(shortCuts);
    }

    @Override
    public void unRegister() {
        super.unRegister();
    }

    @Override
    public void switchMode() {

    }

    @Override
    public void refetch() {
//        noSocket = false;
        noAlarm = false;
        noCamera = false;
        getControlList();
        fetchModeList();
        fetchDeviceList();
    }
}
